#include<boost/asio.hpp>
#include<cstdlib>
#include<deque>
#include<iostream>
#include<thread>
#include "chat_message.h"

using boost::asio::ip::tcp;

using chat_message_queue = std::deque<chat_message>;

class chat_client {
    public:
        chat_client(boost::asio::io_service& io_service,
                    tcp::resolver::iterator endpoint_iterator
        );

        void write(const chat_message& msg); 

        void close();
    private:

        void do_connect(tcp::resolver::iterator endpoint_iterator);


        void do_read_header();

        void do_read_body();
        
        // 向服务端真正发送消息的函数
        void do_write();

        // 注意使用了引用类型,
        // io_service对象的生命周期必须要大于chat_client对象的生命周期
        // 否则会出现引用失效，导致异常
        boost::asio::io_service& io_service_;
        tcp::socket socket_;
        chat_message read_msg_;
        chat_message_queue write_msgs_;
};

